package com.itcm.skymail.member.controller;

import com.itcm.common.utils.PageUtils;
import com.itcm.common.utils.R;
import com.itcm.skymail.member.entity.MemberEntity;
import com.itcm.skymail.member.exception.PhoneExistException;
import com.itcm.skymail.member.exception.UsernameExistException;
import com.itcm.skymail.member.feign.CouponFeignService;
import com.itcm.skymail.member.service.MemberService;
import com.itcm.skymail.member.vo.LoginUser;
import com.itcm.skymail.member.vo.RegistMember;
import com.itcm.skymail.member.vo.WeiboAccessVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 会员
 *
 * @author cm_fighting
 * @email cmtianxie@163.com
 * @date 2020-08-27 16:37:37
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private CouponFeignService feignService;

    @PostMapping("/regist")
    public R regist(@RequestBody RegistMember user) {
        try {
            memberService.regist(user);
        } catch (PhoneExistException | UsernameExistException e) {
            return R.error().put("message", e.getMessage());
        }
        return R.ok();
    }

    @PostMapping("/login")
    public MemberEntity login(@RequestBody LoginUser user) {
        MemberEntity member = memberService.login(user);
//        if (member != null) {
//            return R.ok().put("user", member);
//        } else {
//            return R.error(BizCodeEnum.LOGIN_ACC_PWD_EXCEPTION.getCode(), BizCodeEnum.LOGIN_ACC_PWD_EXCEPTION.getMsg());
//        }
        return member;
    }

    @PostMapping("/weiboLogin")
    public MemberEntity login(@RequestBody WeiboAccessVo vo) {
        MemberEntity member = memberService.weiboLogin(vo);
        return member;
    }

    @GetMapping("/coupons")
    public R test() {
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("唐一");
        R r = feignService.memberCoupons();

        return R.ok().put("member", memberEntity).put("coupons", r.get("coupons"));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
